package com.example.client.controller.topic;

import com.example.commentservice.service.CommentService;
import com.example.commonapi.pojo.Topic;
import com.example.commonapi.pojo.UAC;
import com.example.commonapi.pojo.UAT;
import com.example.commonapi.pojo.User;
import com.example.topicservice.service.TopicService;
import com.example.userservice.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class TopicController {
    private static final Log logger = LogFactory.getLog(TopicController.class);

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Reference(version = "${demo.service.version}")
    private TopicService topicService;

    @Reference(version = "${demo.service.version}")
    private UserService userService;

    @Reference(version = "${demo.service.version}")
    private CommentService commentService;


    @RequestMapping("/topic/edit/{topicId}")
    public String topicEdit(@PathVariable("topicId") String topicId, HttpServletRequest request) {
        Topic topic = topicService.getTopicById(Integer.valueOf(topicId));
        User user = topicService.getUserByTopicId(Integer.valueOf(topicId));

        request.setAttribute("topic", topic);
        request.setAttribute("user", user);

        return "user/user-edit-topic";
    }

    @RequestMapping("/topic/history/{userId}")
    public String topicHistory(@PathVariable("userId") Integer userId, HttpServletRequest request) throws JsonProcessingException {
        List<Topic> topics = topicService.getTopicByUserId(userId);
        User user = userService.getUserById(userId);

        request.setAttribute("topics", topics);
        request.setAttribute("user", user);

        return "user/user-history-topic";
    }

    @GetMapping("/topic/search")
    public String search(@RequestParam("word") String word, HttpServletRequest request){
        List<UAT> uats = topicService.search(word);
        request.setAttribute("uats", uats);
        return "index";
    }

    @RequestMapping("/topic/{id}")
    public String topic(@PathVariable("id") String id, HttpServletRequest request) {
        User user = topicService.getUserByTopicId(Integer.valueOf(id));
        Topic topic = topicService.getTopicById(Integer.valueOf(id));
        List<UAC> uacs = commentService.getUACByTopicId(Integer.valueOf(id));

        // 设置Topic和User到request
        request.setAttribute("topic", topic);
        request.setAttribute("user", user);
        request.setAttribute("uacs", uacs);
        return "topic/topic";
    }

    @RequestMapping("/user/create")
    public String userCreateTopic(HttpServletRequest request) {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        request.setAttribute("user", user);

        return "user/user-create-topic";
    }
}
